package com.bestplus.daqingapp.common.step;

import android.util.Log;

import java.util.Date;
import com.bestplus.daqingapp.BuildConfig;
import com.bestplus.daqingapp.common.util.DataUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.pgyersdk.crash.PgyCrashManager;
/**
 * 根据StepDetector传入的步点"数"步子
 */
public final class StepCount {

    public static final String SAVE_TIME = "save.time";
    public static final String STEP_TODAY = "step.today";


    // private float[] weekCounts = new float[7];
    private float[] weekCounts = new float[31];
    private int count = 1;
    private float mCount = 0;
    private long timeOfLastPeak = 0;
    private long timeOfThisPeak = 0;
    private StepDetector stepDetector;

    public StepCount() {
        stepDetector = new StepDetector();
        stepDetector.init(this);
    }

    public StepDetector getStepDetector(){
        return stepDetector;
    }
    public String getTime() {
        return timeOfThisPeak+","+DataUtil.readLong(Pedometer.getSelf(), SAVE_TIME);
    }

    /**
     * 连续走十步才会开始计步
     * 连续走了9步以下,停留超过3秒,则计数清空
     */
    public void countStep() {
        synchronized(this){
            long saveTime = DataUtil.readLong(Pedometer.getSelf(), SAVE_TIME);
            this.timeOfLastPeak = this.timeOfThisPeak;
            this.timeOfThisPeak = System.currentTimeMillis();
         
            if (this.timeOfThisPeak - this.timeOfLastPeak <= 3000L) {
                if (this.count < 3) {
                    this.count++;
                } else if (this.count == 3) {
                    this.count++;
                    this.mCount += this.count;
                    weekCounts[0] = this.mCount;
                } else {
                    this.mCount++;
                     weekCounts[0] = this.mCount;
                }
            } else {//超时
                this.count = 1;//为1,不是0
            }
            // this.mCount++;
            if ((timeOfThisPeak - saveTime) > 3000) {
                DataUtil.saveFloat(Pedometer.getSelf(), STEP_TODAY, mCount);
            }
            if (!isToday(saveTime)) {
                for(int i = 0; i < weekCounts.length - 1; i++){
                    weekCounts[i+1] = weekCounts[i];
                }
                for(int i = 0; i < weekCounts.length; i++){
                      DataUtil.saveFloat(Pedometer.getSelf(), StepCount.STEP_TODAY+"."+i,weekCounts[i]);
                }
                weekCounts[1] = mCount;
                weekCounts[0] = 0;
                mCount = 1;
                DataUtil.saveFloat(Pedometer.getSelf(), STEP_TODAY, 1);
            }
            DataUtil.saveLong(Pedometer.getSelf(), SAVE_TIME, timeOfThisPeak);

        }
        if (BuildConfig.DEBUG)
            Log.i("log", "today steps: " + mCount);
    }


    public void setSteps(float initValue) {
        this.mCount = initValue;
        for(int i = 0; i < weekCounts.length; i++){
            weekCounts[i]  =DataUtil.readFloat(Pedometer.getSelf(), StepCount.STEP_TODAY+"."+i);
        }
        this.count = 0;
        timeOfLastPeak = 0;
        this.timeOfThisPeak = DataUtil.readLong(Pedometer.getSelf(), SAVE_TIME);

    }

    public float getSteps() {
         if(!this.isToday(System.currentTimeMillis(),this.timeOfThisPeak)){
                    return -1;
                 }
        return mCount + 0.5F;
    }

    public float getSteps(int day) {
        if(day == 0){
             if(!this.isToday(System.currentTimeMillis(),this.timeOfThisPeak)){
                return -1;
              }
            return mCount + 0.5F;
        }
        return weekCounts[day];

    }

    public float getSteps(int steps,String serverDay) {
        synchronized(this){

          if(!isSameDay(serverDay,this.timeOfThisPeak)){
            return -1;
          }
          if(this.mCount >= steps){
            return getSteps();
            // return 1001;
          }else {
            this.mCount = steps;
            weekCounts[0] = steps;
              return getSteps();
            // return 1002;
          }
        }

    }

    public static boolean isToday(long millis) {
        Calendar today = Calendar.getInstance();
        today.setTimeInMillis(System.currentTimeMillis());
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(millis);
        return today.get(Calendar.DAY_OF_YEAR) == time.get(Calendar.DAY_OF_YEAR);
    }


      public static boolean isToday(long millis,long saveDay) {
        Calendar today = Calendar.getInstance();
        today.setTimeInMillis(saveDay);
        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(millis);
        return today.get(Calendar.DAY_OF_YEAR) == time.get(Calendar.DAY_OF_YEAR);
    }

    final static SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

    public static boolean isSameDay(String serverDay,long saveTime){
        if(serverDay == null || serverDay.isEmpty()){
            return false;
        }
        Date date = null;
           try {
            date = sdf.parse(serverDay);
           } catch (ParseException e) {
            e.printStackTrace();
            return false;
           }
        return isToday(date.getTime(),saveTime);

    }

}
